package com.hui.blog_server.service.impl;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 彩色的小灰
 */
@Service
public class FileServiceImpl {

    @Value("${external.images}")
    private String basePath;

    // 方法一：传入文件夹路径，返回该文件夹下所有子文件夹名称
    public List<String> getSubFolderNames(String folderPath) {
        List<String> folderNames = new ArrayList<>();
        File folder = new File(folderPath);
        if (folder.exists() && folder.isDirectory()) {
            Collection<File> subFolders = FileUtils.listFilesAndDirs(folder, DirectoryFileFilter.DIRECTORY, null);
            for (File subFolder : subFolders) {
                if (subFolder.isDirectory()) {
                    folderNames.add(subFolder.getName());
                }
            }
        }
        return folderNames;
    }
}